
#ifndef MY_GCHECKLOCATIONREGSV32
#define MY_GCHECKLOCATIONREGSV32

void gMsgLocationRegSvr32(PWSTR buf, const wchar_t* curr, const wchar_t* right) {
	wcscpy(buf, L"You don't use:\r\n");
	wcscat(buf, curr);
	wcscat(buf, L"\r\n\r\nYou must use:\r\n");
	wcscat(buf, right);
	wcscat(buf, L"\r\n");
	MessageBoxW(
		GetDesktopWindow(),
		buf,
		g_params[g_idDLLPATH].c_str(),
		MB_OK | MB_ICONWARNING
		);
}

/*
There are indeed two version of regsvr32 provided with 64-bit Windows.  
The 64-bit version is in %windir%\system32 and the 32-bit version is in %windir%\SysWoW64.  
If you are registering a 32-bit DLL, use the 32-bit version.
*/

BOOL gCheckLocationRegSvr32() {

	if (!gIsWow64()) return TRUE;
	
	BOOL bResult = TRUE;	
	size_t N = MAX_PATH * 3 * sizeof(WCHAR);
		
	PWSTR buf = (PWSTR)HeapAlloc(
	GetProcessHeap(), 
	HEAP_ZERO_MEMORY,
	N
	);
		
	DWORD res = GetModuleFileNameW(
		GetModuleHandle(NULL),
		buf, 
		N
	);
	
	if (res > 0) {
		
		size_t M = ( wcslen(buf) + 1) * sizeof(WCHAR);

		PWSTR clone = (PWSTR)HeapAlloc(
			GetProcessHeap(), 
			HEAP_ZERO_MEMORY,
			M
		);
		
		wcscpy(clone, buf);

		const wchar_t* for32bit = L"SysWoW64";
		const wchar_t* for64bit = L"System32";	
		
		wchar_t* p_1 = wcsstr(clone, for32bit);
		wchar_t* p_2 = wcsstr(clone, for64bit);
		
		if (p_1 != NULL) wcsncpy(p_1, for64bit, wcslen(for64bit));
		if (p_2 != NULL) wcsncpy(p_2, for32bit, wcslen(for32bit));
		
		wstring current = buf;
		wstring other = clone;
		
		HeapFree(GetProcessHeap(), 0, clone);				
		
		ZeroMemory(buf, N);
		
		#if defined _WIN32
		if (p_2 != NULL) {
			if (gFileExists(other.c_str())) {	
				gMsgLocationRegSvr32(buf, current.c_str(), other.c_str());
				bResult = FALSE;
			};
		};
		#endif
		
		#if defined _WIN64
		if (p_1 != NULL) {
			if (gFileExists(other.c_str())) {	
				gMsgLocationRegSvr32(buf, current.c_str(), other.c_str());			
				bResult = FALSE;
			};
		};
		#endif
		
	} else {
		bResult = FALSE;
	}
	
	HeapFree(GetProcessHeap(), 0, buf);		
	
	return bResult;

}

#endif
